package com.l024.loanthief;

import android.Manifest;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.l024.loanthief.base.activity.BaseActivity;
import com.l024.loanthief.databinding.ActivityMainBinding;
import com.l024.loanthief.views.MainPage.NavFragmentPagerAdapter;
import com.l024.loanthief.views.homePage.HomeFragment;
import com.l024.loanthief.views.myPage.MyFragment;
import com.l024.loanthief.views.utilPage.UtilFragment;
import com.tbruyelle.rxpermissions3.RxPermissions;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private ActivityMainBinding binding;
    private SparseArray<Fragment> fragments;
    private NavFragmentPagerAdapter navFragmentPagerAdapter;
    private static final String[] permissionsGroup=new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,Manifest.permission.READ_SMS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        init();
    }

    private void init() {
        getPermission();
        fragments = new SparseArray<>();
        fragments.append(0,new HomeFragment());
        fragments.append(1,new UtilFragment());
        fragments.append(2,new MyFragment());
        navFragmentPagerAdapter = new NavFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        binding.vpPage.setAdapter(navFragmentPagerAdapter);
        binding.vpPage.setCurrentItem(0);
        addStatusViewWithColor(this, getResources().getColor(R.color.themeColor));
        binding.vpPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedNav(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        binding.rbHome.setOnClickListener(this);
        binding.rbUtil.setOnClickListener(this);
        binding.rbMy.setOnClickListener(this);
    }

    /**
     * 当前选中
     * @param position
     */
    private void selectedNav(int position) {

        binding.vpPage.setCurrentItem(position);
        switch (position){
            case 0:{
                addStatusViewWithColor(this, getResources().getColor(R.color.themeColor));
                binding.rbHome.setChecked(true);
                binding.rbUtil.setChecked(false);
                binding.rbMy.setChecked(false);
                break;
            }
            case 1:{
                addStatusViewWithColor(this, getResources().getColor(R.color.colorPrimary));
                binding.rbHome.setChecked(false);
                binding.rbUtil.setChecked(true);
                binding.rbMy.setChecked(false);
                break;
            }
            case 2:{
                addStatusViewWithColor(this, getResources().getColor(R.color.colorAccent));
                binding.rbHome.setChecked(false);
                binding.rbUtil.setChecked(false);
                binding.rbMy.setChecked(true);
                break;
            }
        }
    }

    public void getPermission(){
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(permissionsGroup)
                .subscribe(permixssion -> {
                   if(permixssion.granted){
                       //授权通过
                   }else if(permixssion.shouldShowRequestPermissionRationale){
                       //拒绝但是没有电不提示
                   }else{
                       // 用户拒绝了该权限，而且选中『不再询问』那么下次启动时，就不会提示出来了
                   }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rb_home:{
                selectedNav(0);
                break;
            }
            case R.id.rb_util:{
                selectedNav(1);
                break;
            }
            case R.id.rb_my:{
                selectedNav(2);
                break;
            }
        }
    }

}
